package com.firebird.app.reader.pdf.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.firebird.app.reader.R
import java.io.File

class PdfFileAdapter(private val listFile: Array<File>?) : RecyclerView.Adapter<PdfFileAdapter.ViewHolder>() {
    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val view = View.inflate(viewGroup.context, R.layout.item_pdf_file, null)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
        val fileName = listFile!![i].name
        viewHolder.fileNameTv?.text = fileName
        viewHolder.fileNameTv?.setOnClickListener { v -> mOnItemClickLitener!!.onItemClick(v, i) }
    }

    override fun getItemCount(): Int {
        return listFile?.size ?: 0
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var fileNameTv: TextView?=null

        init {
            fileNameTv = itemView.findViewById<View>(R.id.ipf_file_name_tv) as TextView
        }
    }

    interface OnItemClickLitener {
        fun onItemClick(view: View?, position: Int)
    }

    private var mOnItemClickLitener: OnItemClickLitener? = null
    fun setOnItemClickLitener(mOnItemClickLitener: OnItemClickLitener?) {
        this.mOnItemClickLitener = mOnItemClickLitener
    }

}